# pstn_recipes.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
package CCNv2::pstn_recipes;
use strict;
use warnings;

# Note that the labels provided are used for documentation only;
# the scripts do not (currently?) use them for anything.

# This should probably be merged/managed together with the information
# in /home/manager/content/e164

# How to build an E.164 number from a PSTN dialing pattern.
# (This is information about local PSTN dialing practices.)
#                                  match                skip  keep     prefix
# (keep = -1 means "all")

our %pstn_to_e164_recipes = (
    'nanpa' => {
        # Note: '11' just means 'pass number as-is' (useful for LD sip trunks)
        'local'         => 
            sub { $_[0] ==  7 ?  [          'NXXXXXX',  0,  7, '1'.$_[1]     ]
                : $_[0] == 10 ?  [  $_[1] . 'NXXXXXX',  0, 10, '1'           ]
                : $_[0] == 11 ?  [      '1NXXNXXXXXX',  0, 11, ''            ]
                : undef
                },
        'longdistance'  => sub { [ '1NXXNXXXXXX',       1, 10, '1'            ] },
        'international' => sub { [ '011.',              3, -1, ''             ] },
        'services'      => sub { [ 'N11',               0,  3, '1'            ] },
        'operator'      => sub { [ '0',                 0,  1, '1'            ] },
        'intloperator'  => sub { [ '00',                0,  2, '1'            ] },
        'emergency'     => sub { [ '911',               3, -1, 'emergency'    ] },
    },
    'fr' => {
        'longdistance'  => sub { [ '0XXXXXXXXXXX',      1, 11, '33'             ] },
        'international' => sub { [ '00.',               2, -1, ''               ] },
        'emergency'     => sub { [ '112',               3, -1, 'emergency'      ] },
    },
    'e164' => {
         'any'          =>  sub { [ 'X.',               0, -1, ''           ] },
    },
);

$pstn_to_e164_recipes{'us'} = $pstn_to_e164_recipes{'nanpa'};
$pstn_to_e164_recipes{'ca'} = $pstn_to_e164_recipes{'nanpa'};
$pstn_to_e164_recipes{'none'} = $pstn_to_e164_recipes{'e164'};
$pstn_to_e164_recipes{'transparent'} = $pstn_to_e164_recipes{'e164'};


# How to build a PSTN number from an E.164 number.
#                                  match                skip  keep     prefix
# (keep = -1 means "all")

our %e164_to_pstn_recipes = (
    'nanpa' => {
        'local'         => 
            sub { $_[0] ==  7 ? [ '1'.$_[1].'NXXXXXX',  4,  7,    ''        ]
                : $_[0] == 10 ? [ '1'.$_[1].'NXXXXXX',  1, 10,    ''        ]
                : $_[0] == 11 ? [       '1NXXNXXXXXX',  1, 11,    ''        ]
                : undef },
        'longdistance'  => sub { [ '1NXXNXXXXXX',       1, 10,    '1'       ] },
        'international' => sub { [ 'N.',                0, -1,    '011'     ] },
        'emergency'     => sub { [ 'emergency',         9,  0,    '911'     ] },
        # The following are needed for SIP trunking
        'VSCN11'        => sub { [ '1N11',              1,  3,    ''        ] },
        'emergency2'    => sub { [ '1911',              4, -1,    '911'     ] },
        'operator'      => sub { [ '10',                1,  1,    ''        ] },
        'intloperator'  => sub { [ '100',               1,  2,    ''        ] },
    },
    'fr' => {
        'longdistance'  => sub { [ '33[1-5]XXXXXXXXXX', 2, 11,    '0'       ] }, # 0 means "pre-selected LD carrier for this trunk"
        'mobile'        => sub { [ '336XXXXXXXXXX',     2, 11,    '0'       ] },
        'international' => sub { [ '[1-9].',            0, -1,    '00'      ] }, # 00 means "pre-selected Intl carrier for this trunk"
        'emergency'     => sub { [ 'emergency',         9, -1,    '112'     ] },
    },
    'e164' => {
        'any'          =>  sub { [ 'X.',               0, -1, ''           ] },
    },
);

$e164_to_pstn_recipes{'us'} = $e164_to_pstn_recipes{'nanpa'};
$e164_to_pstn_recipes{'ca'} = $e164_to_pstn_recipes{'nanpa'};
$e164_to_pstn_recipes{'none'} = $e164_to_pstn_recipes{'e164'};
$e164_to_pstn_recipes{'transparent'} = $e164_to_pstn_recipes{'e164'};

1;
